jQuery.fn.autocomplete =  function (data){
	var b = $(this);	
	url = data.url; 
	css = data.css ? data.css : '';
	postname = data.postname ? data.postname : 'id';
	placeholder = data.placeholder ? data.placeholder : '';
	
	var dialog = false;
	var p = $("input[name='"+postname+"']");
	b.parent().css({'position':'relative'});
	top = b.height();
	if($("#kw_serach").length == 0){
		b.after('<div id="kw_serach" style="display:none;"><ul id="kw_input" style="padding: 10px 15px 0px 15px;"><input placeholder="'+placeholder+'" class="form-control" type="text" /><li></li></ul><ul id="kw_list"></ul></div>');
	}
		
	var input = $("#kw_input input");
	
	$("#kw_serach").css({'position':'absolute','width':'100%','height':'auto','min-height':'100px','top':b,'left':'0px','z-index':'9999','background':'#fff','border':'1px solid #4680d1'});
	
	if(css) $("#kw_serach").css(css);
	
	var request;
	input.keyup(function(){	
		dialog = true;		
		string = b.attr('name') + '=' + $("#kw_input input").val();
		if(request) {
			request.abort();
		}				
		request = $.ajax({
			url:url,
			type:'post',
			dataType:'json',
			data:string,
			cache:false,
			success:function(e){
				var html = '';	
				if(e.length > 0){					
					$.each(e,function(k,v){
						html += '<li class="kw_item" data-mobile="'+v.mobile+'" data-score="'+v.newbonusflg+'" data-money="'+v.newblanceflg+'" data-no="'+v.member_no+'" data-name="'+v.membername+'" data-id="'+v.id+'" data-levelname="'+v.levelname+'">'+v.member_no+"/"+v.membername+'</li>';						
					})				
				}else{
					html += '<li class="kw_item" data-id="0">查询不到您想搜索的内容</li>';
				}
				$("#kw_serach ul#kw_list").html(html);
				$("#kw_serach").show();
				$("#kw_serach ul#kw_list").css({'padding':'10px 0px'});
				$("#kw_serach ul#kw_list li").css({'padding':'5px 15px','font-size':'14px;','cursor':'pointer'});
			}
		});
	})
	
	input.focus(function(){		
		dialog = true;	
	})
	
	input.blur(function(){	
		dialog = false;
		setTimeout(function(){
			if(!dialog){
				$('#kw_serach').fadeOut('slow');
			};
		},100);	
	})
	
	b.focus(function(){
		dialog = true;
		string = b.attr('name') + '=' + $("#kw_input input").val();
		if(request) {
			request.abort();
		}				
		request = $.ajax({
			url:url,
			type:'post',
			dataType:'json',
			data:string,
			cache:false,
			success:function(e){
				var html = '';	
				if(e.length > 0){					
					$.each(e,function(k,v){
						html += '<li class="kw_item" data-mobile="'+v.mobile+'" data-score="'+v.newbonusflg+'" data-money="'+v.newblanceflg+'" data-no="'+v.member_no+'" data-name="'+v.membername+'" data-id="'+v.id+'" data-levelname="'+v.levelname+'">'+v.member_no+"/"+v.mobile+"/"+v.membername+'</li>';						
					})				
				}else{
					html += '<li class="kw_item" data-id="0">查询不到您想搜索的内容</li>';
				}
				$("#kw_serach ul#kw_list").html(html);
				$("#kw_serach").show();
				$("#kw_serach ul#kw_list").css({'padding':'10px 0px'});
				$("#kw_serach ul#kw_list li").css({'padding':'5px 15px','font-size':'14px;','cursor':'pointer'});
			}
		});		
	});
	
	b.blur(function(){
		dialog = false;
		setTimeout(function(){
			if(!dialog){
				$('#kw_serach').fadeOut('slow');
			};
		},1000);		
	})
	
	/*$(document).on('click',function(){
		setTimeout(function(){
			if(!dialog){
				$('#kw_serach').fadeOut('slow');
			};
		},500);		
	})*/
	
	$(document).on('click',"#kw_serach ul li.kw_item",function(){
		if($(this).attr('data-id') !=0 ){
			dialog = false;	
			b.attr('data-id',$(this).attr('data-id'));
			var name=$(this).attr('data-name');
			var no=$(this).attr('data-no');
			var money=$(this).attr('data-money');
			var score=$(this).attr('data-score');
			var mobile=$(this).attr('data-mobile');
			var levelname=$(this).attr('data-levelname');
			$("#name").text(name);
			$("#no").text(no);
			$("#money").text(money);
			$("#score").text(score);
			$("#mobile").text(mobile);
			if(levelname=='null'){
				levelname = '普通会员';
			}
			$("#levelname").text(levelname);
			$("#member_info").show();
			$("#line_member_id").val($(this).attr('data-id'));
			b.val($(this).text());
			$("#kw_serach").fadeOut('slow');	
			var jctable=$("#jici_info").attr("id");
			if(jctable!=undefined){
				$.ajax({
					type:"post",
					data:{member_id:$(this).attr('data-id')},
					url:"/web/cashier/getjici",
					success:function(data){
						var list=data.data;
						if(data.code==200){
							var html="";
							for(var i=0;i<list.length;i++){
							 var jc=list[i];
							 var tr='<tr class="long-tr">'+
				                	'<td>'+jc.name+'</td>'+
				                	'<td>'+jc.num+'</td>'+
				                	'<td>'+jc.xiaofei_num+'</td>'+
				                	'<td><span id="xfnum'+jc.id+'" >0</span><span style="padding-left:20px;font-size:24px" data-gdname="'+jc.goods_names+'"  data-name="'+jc.name+'" data-type="'+jc.type+'" data-num="'+jc.num+'" data-id="'+jc.id+'"  style="font-size: 24px" onclick="select_jici(this)">'+
				                	'<a href="#" class="btn btn-xs btn-primary">添加商品</a></span></td>'+
				                	'</tr>';
				                	html+=tr;
							}		
							$("#jici_info").show();
							$("#no_detail").hide();
							$('#jici_list').html(html);
						}else if(data.code==404){
							var html="没有计次记录";
							$("#jici_info").show();
							$("#no_detail").hide();
							$('#jici_list').html(html);
						}
					}
				});
			}
			if($("input[name='"+postname+"']").val() > 0){
				$("input[name='"+postname+"']").val($(this).attr('data-id'));
			}else{
				b.after('<input type="hidden" name="'+postname+'" value="'+$(this).attr('data-id')+'" />');
			}
		}else{
			dialog = true;
		}
	})
}